package love.yuanmengrobot.qdkaoqin.business;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

import love.yuanmengrobot.qdkaoqin.R;
import love.yuanmengrobot.qdkaoqin.entity.Clazz;
import love.yuanmengrobot.qdkaoqin.util.TeacherConstants;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class StartAttendanceActivity extends AppCompatActivity {
    private String CLAZZ_ID;
    private String CLAZZ_NAME;
    private String MEMBER_ID;
    private String key;
    private String attendanceName;

    private EditText editNameText;

    private Button startbutton;
    private Button cancelbutton;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_number_signin);
        Intent intent = getIntent();
        CLAZZ_ID = intent.getStringExtra(TeacherConstants.CLAZZ_ID);
//        urlClazziddParams = "?clazzid=" + CLAZZ_ID;

//        CLAZZ_NAME = intent.getStringExtra(TeacherConstants.CLAZZ_NAME);
        MEMBER_ID = intent.getStringExtra(TeacherConstants.MEMBERID);
        System.out.println("班级id：" + CLAZZ_ID);
//        System.out.println("班级名称：" + CLAZZ_NAME);
        System.out.println("用户id*：" + MEMBER_ID);
//        edit_text_style
        initView();
    }


    public void initView() {
        editNameText = findViewById(R.id.edit_attendance_name);
        startbutton = findViewById(R.id.bu_start_attendance);
        cancelbutton = findViewById(R.id.bu_cancel_attendance);





        startbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 attendanceName = editNameText.getText().toString();
                if (attendanceName != null && attendanceName.length() > 0) {

                    int intkey = (int) (1 + Math.random() * (9999 - 1000 + 1));
                     key = String.valueOf(intkey);
                    System.out.println(attendanceName + CLAZZ_ID + MEMBER_ID + key + "&&&&&");


                    //开始签到

                    post(attendanceName,CLAZZ_ID,MEMBER_ID,key);


//                  post(attendanceName,CLAZZ_ID,MEMBER_ID,key);
                }else {
                    final AlertDialog alertDialog1 = new AlertDialog.Builder(StartAttendanceActivity.this)
                            .setTitle("警告！")//标题
                            .setMessage("请输入签到名！")//内容
                            .setIcon(R.mipmap.ic_warning)
                            .create();
                    alertDialog1.show();
                }
            }
        });
        cancelbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private okhttp3.Callback callback = new okhttp3.Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
//            Log.e(TAG, "Failed to connect server!");
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response)
                throws IOException {
            if (response.isSuccessful()) {
                final String body = response.body().string();
                System.out.println("67777777776:" + body);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (body.contains("成功")) {
                            final AlertDialog alertDialog1 = new AlertDialog.Builder(StartAttendanceActivity.this)
                                    .setTitle("提示")//标题
                                    .setMessage("发起签到成功！")//内容
                                    .setIcon(R.drawable.ic_baseline_check_24)
                                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {

                                            Intent intent = new Intent(StartAttendanceActivity.this,
                                                    EndAttendanceActivity.class);
                                            intent.putExtra(TeacherConstants.AT_NAME,
                                                    attendanceName);
                                            intent.putExtra(TeacherConstants.CLAZZ_ID,
                                                    CLAZZ_ID);
                                            intent.putExtra(TeacherConstants.AT_KEY,
                                                    key);
                                            startActivity(intent);
                                            finish();
                                        }
                                    })
                                    .create();
                            alertDialog1.show();

//                            finish();
                        } else {
//                            final AlertDialog alertDialog1 = new AlertDialog.Builder(StartAttendanceActivity.this)
//                                    .setTitle("提示")//标题
//                                    .setMessage("发起签到失败！")//内容
//                                    .setIcon(R.mipmap.ic_warning)
//                                    .create();
//                            alertDialog1.show();
                        }
//                        Gson gson = new Gson();
//                        Type jsonType =////////****
//                                new TypeToken<List<Clazz>>() {
//                                }.getType();
//                        List<Clazz> clazzListResponse =
//                                gson.fromJson(body, jsonType);
//                        for (Clazz cl : clazzListResponse) {
//                            clazzAdapter.add(cl);
//                        }
//                        clazzAdapter.notifyDataSetChanged();
                    }
                });
            } else {
            }
        }
    };

    private void post(String atname, String clazzid, String memberid, String key) {
        FormBody.Builder builder = new FormBody.Builder();
        /* 添加5个参数 */
        builder.add("op", "start")
                .add("atname", atname)
                .add("clazzid", clazzid)
                .add("memberid", memberid)
                .add("attype", "数字")
                .add("key", key);
        FormBody body = builder.build();
        Request request = new Request.Builder().url("http://www.yuanmengrobot.love/attendance/Attendance").post(body).build();

        /* 下边和get一样了 */
        try {
            OkHttpClient client = new OkHttpClient();
            client.newCall(request).enqueue(callback);
        } catch (NetworkOnMainThreadException ex) {
            ex.printStackTrace();
        }
    }


}
